<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   严酷的魔王: mxnet：结合R与GPU加速深度学习  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript">
   MathJax.Hub.Config({
          tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]},
          TeX: {equationNumbers: {autoNumber: "AMS"}}
        });
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://cos.name/2016/04/mxnet-r/",
                  host: "http://cos.name",
                  prePath: "http://cos.name",
                  scheme: "http",
                  pathBase: "http://cos.name/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466463350: Accept with keywords: (title(0.333333333333):门户网站,深度学习,统计学,魔王,服务平台,中国, topn(0.633333333333):定义,接下来,深度学习,编译,R语言,数字,工具,函数,遍历,图像识别,模型,参数,随机数,图像,格式,手写,数据,节点,图片,文档,预测,回归,用户,折线图,统计学,方法,代码,分类,神经网络,魔王).-->
 </head>
 <body class="single single-post postid-11789 single-format-standard sidebar" onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    严酷的魔王: mxnet：结合R与GPU加速深度学习
   </h1>
  </div>
  <div class="container" id="xmain">
   <div class="hfeed site" id="page">
    <header class="site-header" id="masthead" role="banner">
     <div id="cos-logo">
      <a href="http://cos.name/">
       <img src="http://cos.name/wp-content/themes/COS-kermesinus/images/headers/cos-logo.png"/>
      </a>
     </div>
     <div class="navbar" id="navbar">
      <nav class="navigation main-navigation" id="site-navigation" role="navigation">
       <h3 class="menu-toggle">
        菜单
       </h3>
       <a class="screen-reader-text skip-link" href="http://cos.name/2016/04/mxnet-r/#content" title="跳至内容">
        跳至内容
       </a>
       <div class="menu-%e6%88%91%e7%9a%84%e8%8f%9c%e5%8d%95-container">
        <ul class="nav-menu" id="menu-%e6%88%91%e7%9a%84%e8%8f%9c%e5%8d%95">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-4746" id="menu-item-4746">
          <a href="http://cos.name">
           主页
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-8120" id="menu-item-8120">
          <a href="http://cos.name/cn">
           论坛
          </a>
          <ul class="sub-menu">
           <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-8124" id="menu-item-8124">
            <a href="http://cos.name/cn/wp-login.php?action=register">
             论坛注册
            </a>
           </li>
           <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-8125" id="menu-item-8125">
            <a href="http://cos.name/cn/wp-login.php">
             论坛登录
            </a>
           </li>
          </ul>
         </li>
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8110" id="menu-item-8110">
          <a href="http://cos.name/books/">
           图书资料
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-8112" id="menu-item-8112">
          <a href="http://cos.name/videos">
           视频教程
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-8333" id="menu-item-8333">
          <a href="http://cos.name/salon/">
           统计沙龙
          </a>
         </li>
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8111" id="menu-item-8111">
          <a href="http://cos.name/chinar/">
           R语言会议
          </a>
         </li>
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8109" id="menu-item-8109">
          <a href="http://cos.name/training/">
           讲座与培训
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-8115" id="menu-item-8115">
          <a href="http://cos.name/cn/forum/comprehensive/job/">
           招聘信息
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-4780" id="menu-item-4780">
          <a href="http://cos.name/about">
           关于我们
          </a>
          <ul class="sub-menu">
           <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-8113" id="menu-item-8113">
            <a href="http://cos.name/2008/11/how-to-work-with-cos/">
             加入我们
            </a>
           </li>
           <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-8119" id="menu-item-8119">
            <a href="http://cos.name/donate/">
             赞助我们
            </a>
           </li>
           <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-8114" id="menu-item-8114">
            <a href="http://cos.name/cn/forum/26">
             项目合作
            </a>
           </li>
          </ul>
         </li>
        </ul>
       </div>
       <form action="http://cos.name/" class="search-form" method="get" role="search">
        <label>
         <span class="screen-reader-text">
          搜索：
         </span>
        </label>
       </form>
      </nav>
      <!-- #site-navigation -->
     </div>
     <!-- #navbar -->
    </header>
    <!-- #masthead -->
    <div class="site-main" id="main">
     <div class="content-area" id="primary">
      <div class="site-content" id="content" role="main">
       <article class="post-11789 post type-post status-publish format-standard hentry category-website tag-boosting tag-dmlc tag-r-language tag-xgboost tag-861" id="post-11789">
        <header class="entry-header">
         <h1 class="entry-title">
          mxnet：结合R与GPU加速深度学习
         </h1>
         <div class="entry-meta">
          <span class="date">
           <a href="http://cos.name/2016/04/mxnet-r/" rel="bookmark" title="链向mxnet：结合R与GPU加速深度学习的固定链接">
            <time class="entry-date" datetime="2016-04-07T10:13:38+00:00">
             2016/04/07
            </time>
           </a>
          </span>
          <span class="categories-links">
           <a href="http://cos.name/category/website/" rel="category tag">
            统计之都
           </a>
          </span>
          <span class="tags-links">
           <a href="http://cos.name/tag/boosting/" rel="tag">
            boosting
           </a>
           、
           <a href="http://cos.name/tag/dmlc/" rel="tag">
            dmlc
           </a>
           、
           <a href="http://cos.name/tag/r-language/" rel="tag">
            R语言
           </a>
           、
           <a href="http://cos.name/tag/xgboost/" rel="tag">
            xgboost
           </a>
           、
           <a href="http://cos.name/tag/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0/" rel="tag">
            深度学习
           </a>
          </span>
          <span class="author vcard">
           <a class="url fn n" href="http://cos.name/author/hetong007/" rel="author" title="查看所有由严酷的魔王发布的文章">
            严酷的魔王
           </a>
          </span>
         </div>
         <!-- .entry-meta -->
        </header>
        <!-- .entry-header -->
        <div class="entry-content">
         <p>
          近年来，深度学习可谓是机器学习方向的明星概念，不同的模型分别在图像处理与自然语言处理等任务中取得了前所未有的好成绩。在实际的应用中，大家除了关心模型的准确度，还常常希望能比较快速地完成模型的训练。一个常用的加速手段便是将模型放在GPU上进行训练。然而由于种种原因，R语言似乎缺少一个能够在GPU上训练深度学习模型的程序包。
         </p>
         <p>
          <img src="https://raw.githubusercontent.com/dmlc/dmlc.github.io/master/img/logo-m/mxnetR.png"/>
         </p>
         <p>
          DMLC（Distributed (Deep) Machine Learning Community）是由一群极客发起的组织，主要目标是提供快速高质量的开源机器学习工具。近来流行的boosting模型xgboost便是出自这个组织。最近DMLC开源了一个深度学习工具mxnet，这个工具含有R，python，julia等语言的接口。本文以R接口为主，向大家介绍这个工具的性能与使用方法。
         </p>
         <h1>
          一、五分钟入门指南
         </h1>
         <p>
          在这一节里，我们在一个样例数据上介绍mxnet的基本使用方法。目前mxnet还没有登录CRAN的计划，所以安装方法要稍微复杂一些。
         </p>
         <ul>
          <li>
           如果你是Windows/Mac用户，那么可以通过下面的代码安装预编译的版本。这个版本会每周进行预编译，不过为了保证兼容性，只能使用CPU训练模型。
          </li>
         </ul>
         <pre><code>install.packages(<span class="pl-s"><span class="pl-pds">"</span>drat<span class="pl-pds">"</span></span>, <span class="pl-v">repos</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">"</span>https://cran.rstudio.com<span class="pl-pds">"</span></span>)
<span class="pl-e">drat</span><span class="pl-k">:::</span>addRepo(<span class="pl-s"><span class="pl-pds">"</span>dmlc<span class="pl-pds">"</span></span>)
install.packages(<span class="pl-s"><span class="pl-pds">"</span>mxnet<span class="pl-pds">"</span></span>)</code></pre>
         <ul>
          <li>
           如果你是Linux用户或者想尝试GPU版本，请参考
           <a href="http://mxnet.readthedocs.org/en/latest/build.html#r-package-installation">
            这个链接里
           </a>
           的详细编译教程在本地进行编译。
          </li>
         </ul>
         <p>
          安装完毕之后，我们就可以开始训练模型了，下面两个小节分别介绍两种不同的训练神经网络的方法。
         </p>
         <h3>
          二分类模型与mx.mlp
         </h3>
         <p>
          首先，我们准备一份数据，并进行简单的预处理：
         </p>
         <pre><code>require(mlbench)
require(mxnet)
data(Sonar, package="mlbench")
Sonar[,61] = as.numeric(Sonar[,61])-1
train.ind = c(1:50, 100:150)
train.x = data.matrix(Sonar[train.ind, 1:60])
train.y = Sonar[train.ind, 61]
test.x = data.matrix(Sonar[-train.ind, 1:60])
test.y = Sonar[-train.ind, 61]</code></pre>
         <p>
          我们借用
          <code>
           mlbench
          </code>
          包中的一个二分类数据，并且将它分成训练集和测试集。
          <code>
           mxnet
          </code>
          提供了一个训练多层神经网络的函数
          <code>
           mx.mlp
          </code>
          ，我们额可以通过它来训练一个神经网络模型。下面是
          <code>
           mx.mlp
          </code>
          中的部分参数：
         </p>
         <ul>
          <li>
           训练数据与预测变量
          </li>
          <li>
           每个隐藏层的大小
          </li>
          <li>
           输出层的结点数
          </li>
          <li>
           激活函数类型
          </li>
          <li>
           损失函数类型
          </li>
          <li>
           进行训练的硬件（CPU还是GPU）
          </li>
          <li>
           其他传给mx.model.FeedForward.create的高级参数
          </li>
         </ul>
         <p>
          了解了大致参数后，我们就可以理解并让R运行下面的代码进行训练了。
         </p>
         <pre><code>mx.set.seed<span class="p">(</span><span class="m">0</span><span class="p">)</span>
model <span class="o">&lt;-</span> mx.mlp<span class="p">(</span>train.x<span class="p">,</span> train.y<span class="p">,</span> hidden_node<span class="o">=</span><span class="m">10</span><span class="p">,</span> out_node<span class="o">=</span><span class="m">2</span><span class="p">,</span>      out_activation<span class="o">=</span><span class="s">"softmax"</span><span class="p">, </span>num.round<span class="o">=</span><span class="m">20</span><span class="p">,</span> array.batch.size<span class="o">=</span><span class="m">15</span><span class="p">,</span> learning.rate<span class="o">=</span><span class="m">0.07</span><span class="p">,</span> momentum<span class="o">=</span><span class="m">0.9</span><span class="p">,</span> eval.metric<span class="o">=</span>mx.metric.accuracy<span class="p">)</span></code></pre>
         <pre><code>## Auto detect layout of input matrix, use rowmajor..
## Start training with 1 devices
## [1] Train-accuracy=0.488888888888889
## [2] Train-accuracy=0.514285714285714
## [3] Train-accuracy=0.514285714285714

...

## [18] Train-accuracy=0.838095238095238
## [19] Train-accuracy=0.838095238095238
## [20] Train-accuracy=0.838095238095238</code></pre>
         <p>
          这里要注意使用mx.set.seed而不是R自带的set.seed函数来控制随机数。因为mxnet的训练过程可能会运行在不同的运算硬件上，我们需要一个足够快的随机数生成器来管理整个随机数生成的过程。模型训练好之后，我们可以很简单地进行预测：
         </p>
         <div class="highlight">
          <pre><code class="language-r">preds <span class="o">=</span> predict<span class="p">(</span>model<span class="p">,</span> test.x<span class="p">)</span>
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-text">## Auto detect layout of input matrix, use rowmajor..
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-r">pred.label <span class="o">=</span> <span class="kp">max.col</span><span class="p">(</span><span class="kp">t</span><span class="p">(</span>preds<span class="p">))</span><span class="m">-1</span>
<span class="kp">table</span><span class="p">(</span>pred.label<span class="p">,</span> test.y<span class="p">)</span>
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-text">##           test.y
## pred.label  0  1
##          0 24 14
##          1 36 33</code></pre>
         </div>
         <p>
          如果进行的是多分类预测，mxnet的输出格式是类数X样本数。
         </p>
         <h3>
          回归模型与自定义神经网络
         </h3>
         <p>
          mx.mlp接口固然很方便，但是神经网络的一大特点便是它的灵活性，不同的结构可能有着完全不同的特性。mxnet的亮点之一便是它赋予了用户极大的自由度，从而可以任意定义需要的神经网络结构。我们在这一节用一个简单的回归任务介绍相关的语法。
         </p>
         <p>
          首先，我们仍然要准备好一份数据。
         </p>
         <pre><code class="language-r">data<span class="p">(</span>BostonHousing<span class="p">,</span> package<span class="o">=</span><span class="s">"mlbench"</span><span class="p">)</span>

train.ind <span class="o">=</span> <span class="kp">seq</span><span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="m">506</span><span class="p">,</span> <span class="m">3</span><span class="p">)</span>
train.x <span class="o">=</span> <span class="kp">data.matrix</span><span class="p">(</span>BostonHousing<span class="p">[</span>train.ind<span class="p">,</span> <span class="m">-14</span><span class="p">])</span>
train.y <span class="o">=</span> BostonHousing<span class="p">[</span>train.ind<span class="p">,</span> <span class="m">14</span><span class="p">]</span>
test.x <span class="o">=</span> <span class="kp">data.matrix</span><span class="p">(</span>BostonHousing<span class="p">[</span><span class="o">-</span>train.ind<span class="p">,</span> <span class="m">-14</span><span class="p">])</span>
test.y <span class="o">=</span> BostonHousing<span class="p">[</span><span class="o">-</span>train.ind<span class="p">,</span> <span class="m">14</span><span class="p">]</span></code></pre>
         <p>
          mxnet提供了一个叫做“Symbol”的系统，从而使我们可以定义结点之间的连接方式与激活函数等参数。下面是一个定义没有隐藏层神经网络的简单例子：
         </p>
         <pre><code class="language-r"><span class="c1"># 定义输入数据</span>
data <span class="o">&lt;-</span> mx.symbol.Variable<span class="p">(</span><span class="s">"data"</span><span class="p">)</span>
<span class="c1"># 完整连接的隐藏层</span>
<span class="c1"># data: 输入源</span>
<span class="c1"># num_hidden: 该层的节点数</span>
fc1 <span class="o">&lt;-</span> mx.symbol.FullyConnected<span class="p">(</span>data<span class="p">,</span> num_hidden<span class="o">=</span><span class="m">1</span><span class="p">)</span>

<span class="c1"># 针对回归任务，定义损失函数</span>
lro <span class="o">&lt;-</span> mx.symbol.LinearRegressionOutput<span class="p">(</span>fc1<span class="p">)</span></code></pre>
         <p>
          在神经网络中，回归与分类的差别主要在于输出层的损失函数。这里我们使用了平方误差来训练模型。希望能更进一步了解Symbol的读者可以继续阅读这两份分别以
          <a href="http://mxnet.readthedocs.org/en/latest/R-package/ndarrayAndSymbolTutorial.html">
           代码
          </a>
          和
          <a href="http://mxnet.readthedocs.org/en/latest/python/symbol_in_pictures.html">
           配图
          </a>
          为主的文档。
         </p>
         <p>
          定义了神经网络之后，我们便可以使用mx.model.FeedForward.create进行训练了。
         </p>
         <div class="highlight">
          <pre><code class="language-r">mx.set.seed<span class="p">(</span><span class="m">0</span><span class="p">)</span>
model <span class="o">&lt;-</span> mx.model.FeedForward.create<span class="p">(</span>lro<span class="p">,</span> X<span class="o">=</span>train.x<span class="p">,</span> y<span class="o">=</span>train.y<span class="p">, </span>ctx<span class="o">=</span>mx.cpu<span class="p">(),</span> num.round<span class="o">=</span><span class="m">50</span><span class="p">,</span> array.batch.size<span class="o">=</span><span class="m">20</span><span class="p">, </span>learning.rate<span class="o">=</span><span class="m">2e-6</span><span class="p">,</span> momentum<span class="o">=</span><span class="m">0.9</span><span class="p">,</span> eval.metric<span class="o">=</span>mx.metric.rmse<span class="p">)</span>
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-text">## Auto detect layout of input matrix, use rowmajor..
## Start training with 1 devices
## [1] Train-rmse=16.063282524034
## [2] Train-rmse=12.2792375712573
## [3] Train-rmse=11.1984634005885

...

## [48] Train-rmse=8.26890902770415
## [49] Train-rmse=8.25728089053853
## [50] Train-rmse=8.24580511500735</code></pre>
         </div>
         <p>
          这里我们还针对回归任务修改了eval.metric参数。目前我们提供的评价函数包括”accuracy”，”rmse”，”mae” 和 “rmsle”，用户也可以针对需要自定义评价函数，例如：
         </p>
         <pre><code class="language-r">demo.metric.mae <span class="o">&lt;-</span> mx.metric.custom<span class="p">(</span><span class="s">"mae"</span><span class="p">,</span> <span class="kr">function</span><span class="p">(</span>label<span class="p">,</span> pred<span class="p">)</span> <span class="p">{</span>
  res <span class="o">&lt;-</span> <span class="kp">mean</span><span class="p">(</span><span class="kp">abs</span><span class="p">(</span>label<span class="o">-</span>pred<span class="p">))</span>
  <span class="kr">return</span><span class="p">(</span>res<span class="p">)</span>
<span class="p">})</span></code></pre>
         <div class="highlight">
          <pre><code class="language-r">mx.set.seed<span class="p">(</span><span class="m">0</span><span class="p">)</span>
model <span class="o">&lt;-</span> mx.model.FeedForward.create<span class="p">(</span>lro<span class="p">,</span> X<span class="o">=</span>train.x<span class="p">,</span> y<span class="o">=</span>train.y<span class="p">, </span>ctx<span class="o">=</span>mx.cpu<span class="p">(),</span> num.round<span class="o">=</span><span class="m">50</span><span class="p">,</span> array.batch.size<span class="o">=</span><span class="m">20</span><span class="p">, </span>learning.rate<span class="o">=</span><span class="m">2e-6</span><span class="p">,</span> momentum<span class="o">=</span><span class="m">0.9</span><span class="p">,</span> eval.metric<span class="o">=</span>demo.metric.mae<span class="p">)</span>
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-text">## Auto detect layout of input matrix, use rowmajor..
## Start training with 1 devices
## [1] Train-mae=13.1889538083225
## [2] Train-mae=9.81431959337658
## [3] Train-mae=9.21576419870059

...

## [48] Train-mae=6.41731406417158
## [49] Train-mae=6.41011292926139
## [50] Train-mae=6.40312503493494</code></pre>
         </div>
         <p>
          至此，你已经掌握了基本的mxnet使用方法。接下来，我们将介绍更好玩的应用。
         </p>
         <h1>
          二、手写数字竞赛
         </h1>
         <p>
          在这一节里，我们以Kaggle上的
          <a href="https://www.kaggle.com/c/digit-recognizer">
           手写数字数据集（MNIST）竞赛
          </a>
          为例子，介绍如何通过mxnet定义一个强大的神经网络，并在GPU上快速训练模型。
         </p>
         <p>
          第一步，我们从Kaggle上
          <a href="https://www.kaggle.com/c/digit-recognizer/data">
           下载数据
          </a>
          ，并将它们放入data/文件夹中。然后我们读入数据，并做一些预处理工作。
         </p>
         <div class="highlight">
          <pre><code class="language-r"><span class="kn">require</span><span class="p">(</span>mxnet<span class="p">)</span></code>
train <span class="o">&lt;-</span> read.csv<span class="p">(</span><span class="s">'data/train.csv'</span><span class="p">,</span> header<span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span> 
test <span class="o">&lt;-</span> read.csv<span class="p">(</span><span class="s">'data/test.csv'</span><span class="p">,</span> header<span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span> 
train <span class="o">&lt;-</span> <span class="kp">data.matrix</span><span class="p">(</span>train<span class="p">)</span> 
test <span class="o">&lt;-</span> <span class="kp">data.matrix</span><span class="p">(</span>test<span class="p">)</span> 

train.x <span class="o">&lt;-</span> train<span class="p">[,</span><span class="m">-1</span><span class="p">]</span> 
train.y <span class="o">&lt;-</span> train<span class="p">[,</span><span class="m">1</span><span class="p">]

</span><code class="language-r">train.x <span class="o">&lt;-</span> <span class="kp">t</span><span class="p">(</span>train.x<span class="o">/</span><span class="m">255</span><span class="p">)</span>
test <span class="o">&lt;-</span> <span class="kp">t</span><span class="p">(</span>test<span class="o">/</span><span class="m">255</span><span class="p">)</span></code></pre>
         </div>
         <p>
          最后两行预处理的作用有两个：
         </p>
         <ul>
          <li>
           原始灰度图片数值处在[0,255]之间，我们将其变换到[0,1]之间。
          </li>
          <li>
           mxnet接受 像素X图片 的输入格式，所以我们对输入矩阵进行了转置。
          </li>
         </ul>
         <p>
          接下来我们定义一个特别的神经网络结构：
          <a href="http://yann.lecun.com/exdb/lenet/">
           LeNet
          </a>
          。这是Yann LeCun提出用于识别手写数字的结构，也是最早的卷积神经网络之一。同样的，我们使用Symbol语法来定义，不过这次结构会比较复杂。
         </p>
         <pre><code class="language-r"><span class="c1"># input</span>
data <span class="o">&lt;-</span> mx.symbol.Variable<span class="p">(</span><span class="s">'data'</span><span class="p">)</span>
<span class="c1"># first conv</span>
conv1 <span class="o">&lt;-</span> mx.symbol.Convolution<span class="p">(</span>data<span class="o">=</span>data<span class="p">,</span> kernel<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="m">5</span><span class="p">,</span><span class="m">5</span><span class="p">),</span> num_filter<span class="o">=</span><span class="m">20</span><span class="p">)</span>
tanh1 <span class="o">&lt;-</span> mx.symbol.Activation<span class="p">(</span>data<span class="o">=</span>conv1<span class="p">,</span> act_type<span class="o">=</span><span class="s">"tanh"</span><span class="p">)</span>
pool1 <span class="o">&lt;-</span> mx.symbol.Pooling<span class="p">(</span>data<span class="o">=</span>tanh1<span class="p">,</span> pool_type<span class="o">=</span><span class="s">"max"</span><span class="p">,</span>
                          kernel<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">2</span><span class="p">),</span> stride<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">2</span><span class="p">))</span>
<span class="c1"># second conv</span>
conv2 <span class="o">&lt;-</span> mx.symbol.Convolution<span class="p">(</span>data<span class="o">=</span>pool1<span class="p">,</span> kernel<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="m">5</span><span class="p">,</span><span class="m">5</span><span class="p">),</span> num_filter<span class="o">=</span><span class="m">50</span><span class="p">)</span>
tanh2 <span class="o">&lt;-</span> mx.symbol.Activation<span class="p">(</span>data<span class="o">=</span>conv2<span class="p">,</span> act_type<span class="o">=</span><span class="s">"tanh"</span><span class="p">)</span>
pool2 <span class="o">&lt;-</span> mx.symbol.Pooling<span class="p">(</span>data<span class="o">=</span>tanh2<span class="p">,</span> pool_type<span class="o">=</span><span class="s">"max"</span><span class="p">,</span>
                          kernel<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">2</span><span class="p">),</span> stride<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">2</span><span class="p">))</span>
<span class="c1"># first fullc</span>
flatten <span class="o">&lt;-</span> mx.symbol.Flatten<span class="p">(</span>data<span class="o">=</span>pool2<span class="p">)</span>
fc1 <span class="o">&lt;-</span> mx.symbol.FullyConnected<span class="p">(</span>data<span class="o">=</span>flatten<span class="p">,</span> num_hidden<span class="o">=</span><span class="m">500</span><span class="p">)</span>
tanh3 <span class="o">&lt;-</span> mx.symbol.Activation<span class="p">(</span>data<span class="o">=</span>fc1<span class="p">,</span> act_type<span class="o">=</span><span class="s">"tanh"</span><span class="p">)</span>
<span class="c1"># second fullc</span>
fc2 <span class="o">&lt;-</span> mx.symbol.FullyConnected<span class="p">(</span>data<span class="o">=</span>tanh3<span class="p">,</span> num_hidden<span class="o">=</span><span class="m">10</span><span class="p">)</span>
<span class="c1"># loss</span>
lenet <span class="o">&lt;-</span> mx.symbol.SoftmaxOutput<span class="p">(</span>data<span class="o">=</span>fc2<span class="p">)</span></code></pre>
         <p>
          为了让输入数据的格式能对应LeNet，我们要将数据变成R中的array格式：
         </p>
         <pre><code class="language-r">train.array <span class="o">&lt;-</span> train.x
<span class="kp">dim</span><span class="p">(</span>train.array<span class="p">)</span> <span class="o">&lt;-</span> <span class="kt">c</span><span class="p">(</span><span class="m">28</span><span class="p">,</span> <span class="m">28</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="kp">ncol</span><span class="p">(</span>train.x<span class="p">))</span>
test.array <span class="o">&lt;-</span> test
<span class="kp">dim</span><span class="p">(</span>test.array<span class="p">)</span> <span class="o">&lt;-</span> <span class="kt">c</span><span class="p">(</span><span class="m">28</span><span class="p">,</span> <span class="m">28</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="kp">ncol</span><span class="p">(</span>test<span class="p">))</span></code></pre>
         <p>
          接下来我们将要分别使用CPU和GPU来训练这个模型，从而展现不同的训练效率。
         </p>
         <pre><code class="language-r">n.gpu <span class="o">&lt;-</span> <span class="m">1</span>
device.cpu <span class="o">&lt;-</span> mx.cpu<span class="p">()</span>
device.gpu <span class="o">&lt;-</span> <span class="kp">lapply</span><span class="p">(</span><span class="m">0</span><span class="o">:</span><span class="p">(</span>n.gpu<span class="m">-1</span><span class="p">),</span> <span class="kr">function</span><span class="p">(</span>i<span class="p">)</span> <span class="p">{</span>
  mx.gpu<span class="p">(</span>i<span class="p">)</span>
<span class="p">})</span></code></pre>
         <p>
          我们可以将GPU的每个核以list的格式传递进去，如果有BLAS等自带矩阵运算并行的库存在，则没必要对CPU这么做了。
         </p>
         <p>
          我们先在CPU上进行训练，这次我们只进行一次迭代：
         </p>
         <div class="highlight">
          <pre><code class="language-r">mx.set.seed<span class="p">(</span><span class="m">0</span><span class="p">)</span>
tic <span class="o">&lt;-</span> <span class="kp">proc.time</span><span class="p">()</span>
model <span class="o">&lt;-</span> mx.model.FeedForward.create<span class="p">(</span>lenet<span class="p">,</span> X<span class="o">=</span>train.array<span class="p">,</span> y<span class="o">=</span>train.y<span class="p">, </span>ctx<span class="o">=</span>device.cpu<span class="p">,</span> num.round<span class="o">=</span><span class="m">1</span><span class="p">,</span> array.batch.size<span class="o">=</span><span class="m">100</span><span class="p">, </span>learning.rate<span class="o">=</span><span class="m">0.05</span><span class="p">,</span> momentum<span class="o">=</span><span class="m">0.9</span><span class="p">,</span> wd<span class="o">=</span><span class="m">0.00001</span><span class="p">, </span>eval.metric<span class="o">=</span>mx.metric.accuracy<span class="p">, </span>epoch.end.callback<span class="o">=</span>mx.callback.log.train.metric<span class="p">(</span><span class="m">100</span><span class="p">))</span>
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-text">## Start training with 1 devices
## Batch [100] Train-accuracy=0.1066
## Batch [200] Train-accuracy=0.16495
## Batch [300] Train-accuracy=0.401766666666667
## Batch [400] Train-accuracy=0.537675
## [1] Train-accuracy=0.557136038186157
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-r"><span class="kp">print</span><span class="p">(</span><span class="kp">proc.time</span><span class="p">()</span> <span class="o">-</span> tic<span class="p">)</span>
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-text">##    user  system elapsed
## 130.030 204.976  83.821</code></pre>
         </div>
         <p>
          在CPU上训练一次迭代一共花了83秒。接下来我们在GPU上训练5次迭代：
         </p>
         <div class="highlight">
          <pre><code class="language-r">mx.set.seed<span class="p">(</span><span class="m">0</span><span class="p">)</span>
tic <span class="o">&lt;-</span> <span class="kp">proc.time</span><span class="p">()</span>
model <span class="o">&lt;-</span> mx.model.FeedForward.create<span class="p">(</span>lenet<span class="p">,</span> X<span class="o">=</span>train.array<span class="p">,</span> y<span class="o">=</span>train.y<span class="p">, </span>ctx<span class="o">=</span>device.gpu<span class="p">,</span> num.round<span class="o">=</span><span class="m">5</span><span class="p">,</span> array.batch.size<span class="o">=</span><span class="m">100</span><span class="p">, </span>learning.rate<span class="o">=</span><span class="m">0.05</span><span class="p">,</span> momentum<span class="o">=</span><span class="m">0.9</span><span class="p">,</span> wd<span class="o">=</span><span class="m">0.00001</span><span class="p">, </span>eval.metric<span class="o">=</span>mx.metric.accuracy<span class="p">, </span>epoch.end.callback<span class="o">=</span>mx.callback.log.train.metric<span class="p">(</span><span class="m">100</span><span class="p">))</span>
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-text">## Start training with 1 devices
## Batch [100] Train-accuracy=0.1066
## Batch [200] Train-accuracy=0.1596
## Batch [300] Train-accuracy=0.3983
## Batch [400] Train-accuracy=0.533975
## [1] Train-accuracy=0.553532219570405
## Batch [100] Train-accuracy=0.958
## Batch [200] Train-accuracy=0.96155
## Batch [300] Train-accuracy=0.966100000000001
## Batch [400] Train-accuracy=0.968550000000003
## [2] Train-accuracy=0.969071428571432
## Batch [100] Train-accuracy=0.977
## Batch [200] Train-accuracy=0.97715
## Batch [300] Train-accuracy=0.979566666666668
## Batch [400] Train-accuracy=0.980900000000003
## [3] Train-accuracy=0.981309523809527
## Batch [100] Train-accuracy=0.9853
## Batch [200] Train-accuracy=0.985899999999999
## Batch [300] Train-accuracy=0.986966666666668
## Batch [400] Train-accuracy=0.988150000000002
## [4] Train-accuracy=0.988452380952384
## Batch [100] Train-accuracy=0.990199999999999
## Batch [200] Train-accuracy=0.98995
## Batch [300] Train-accuracy=0.990600000000001
## Batch [400] Train-accuracy=0.991325000000002
## [5] Train-accuracy=0.991523809523812
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-r"><span class="kp">print</span><span class="p">(</span><span class="kp">proc.time</span><span class="p">()</span> <span class="o">-</span> tic<span class="p">)</span>
</code></pre>
         </div>
         <div class="highlight">
          <pre><code class="language-text">##    user  system elapsed
##   9.288   1.680   6.889</code></pre>
         </div>
         <p>
          在GPU上训练5轮迭代只花了不到7秒，快了数十倍！可以看出，对于这样的网络结构，GPU的加速效果是非常显著的。有了快速训练的办法，我们便可以很快的做预测，并且提交到Kaggle上了：
         </p>
         <pre><code class="language-r">preds <span class="o">&lt;-</span> predict<span class="p">(</span>model<span class="p">,</span> test.array<span class="p">)</span>
pred.label <span class="o">&lt;-</span> <span class="kp">max.col</span><span class="p">(</span><span class="kp">t</span><span class="p">(</span>preds<span class="p">))</span> <span class="o">-</span> <span class="m">1</span>
submission <span class="o">&lt;-</span> <span class="kt">data.frame</span><span class="p">(</span>ImageId<span class="o">=</span><span class="m">1</span><span class="o">:</span><span class="kp">ncol</span><span class="p">(</span>test<span class="p">),</span> Label<span class="o">=</span>pred.label<span class="p">)</span>
write.csv<span class="p">(</span>submission<span class="p">,</span> file<span class="o">=</span><span class="s">'submission.csv'</span><span class="p">,</span> row.names<span class="o">=</span><span class="kc">FALSE</span><span class="p">,</span> quote<span class="o">=</span><span class="kc">FALSE</span><span class="p">)</span></code></pre>
         <h1>
          三、图像识别应用
         </h1>
         <p>
          其实对于神经网络当前的应用场景而言，识别手写数字已经不够看了。早些时候，Google公开了一个云API，让用户能够检测一幅图像里面的内容。现在我们提供一个教程，让大家能够自制一个图像识别的在线应用。
         </p>
         <p>
          DMLC用在ImageNet数据集上训练了一个模型，能够直接拿来对真实图片进行分类。同时，我们搭建了一个Shiny应用，只需要不超过150行R代码就能够自己在浏览器中进行图像中的物体识别。
         </p>
         <p>
          为了搭建这个应用，我们要安装shiny和imager两个R包：
         </p>
         <pre><code class="language-r">install.packages("shiny", repos="https://cran.rstudio.com")
install.packages("imager", repos="https://cran.rstudio.com")</code></pre>
         <p>
          现在你已经配置好了mxnet, shiny和imager三个R包，最困难的部分已经完成了！下一步则是让shiny直接下载并运行我们准备好的代码：
         </p>
         <pre><code class="language-r">shiny::runGitHub("thirdwing/mxnet_shiny")</code></pre>
         <p>
          第一次运行这个命令会花上几分钟时间下载预先训练好的模型。训练的模型是Inception-BatchNorm Network，如果读者对它感兴趣，可以阅读这篇文章。准备就绪之后，你的浏览器中会出现一个网页应用，就用本地或在线图片来挑战它吧！
         </p>
         <p>
          如果你只需要一个图像识别的模块，那么我们下面给出最简单的一段R代码让你能进行图像识别。首先，我们要导入预训练过的模型文件：
         </p>
         <pre><code class="language-r">model <span class="o">&lt;&lt;-</span> mx.model.load<span class="p">(</span><span class="s">"Inception/Inception_BN"</span><span class="p">,</span> iteration <span class="o">=</span> <span class="m">39</span><span class="p">)</span>
synsets <span class="o">&lt;&lt;-</span> <span class="kp">readLines</span><span class="p">(</span><span class="s">"Inception/synset.txt"</span><span class="p">)</span>
mean.img <span class="o">&lt;&lt;-</span> <span class="kp">as.array</span><span class="p">(</span>mx.nd.load<span class="p">(</span><span class="s">"Inception/mean_224.nd"</span><span class="p">)[[</span><span class="s">"mean_img"</span><span class="p">]])</span></code></pre>
         <p>
          接下来我们使用一个函数对图像进行预处理，这个步骤对于神经网络模型而言至关重要。
         </p>
         <pre><code class="language-r">preproc.image <span class="o">&lt;-</span> <span class="kr">function</span><span class="p">(</span>im<span class="p">,</span> mean.image<span class="p">)</span> <span class="p">{</span>
  <span class="c1"># crop the image</span>
  shape <span class="o">&lt;-</span> <span class="kp">dim</span><span class="p">(</span>im<span class="p">)</span>
  short.edge <span class="o">&lt;-</span> <span class="kp">min</span><span class="p">(</span>shape<span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">2</span><span class="p">])</span>
  yy <span class="o">&lt;-</span> <span class="kp">floor</span><span class="p">((</span>shape<span class="p">[</span><span class="m">1</span><span class="p">]</span> <span class="o">-</span> short.edge<span class="p">)</span> <span class="o">/</span> <span class="m">2</span><span class="p">)</span> <span class="o">+</span> <span class="m">1</span>
  yend <span class="o">&lt;-</span> yy <span class="o">+</span> short.edge <span class="o">-</span> <span class="m">1</span>
  xx <span class="o">&lt;-</span> <span class="kp">floor</span><span class="p">((</span>shape<span class="p">[</span><span class="m">2</span><span class="p">]</span> <span class="o">-</span> short.edge<span class="p">)</span> <span class="o">/</span> <span class="m">2</span><span class="p">)</span> <span class="o">+</span> <span class="m">1</span>
  xend <span class="o">&lt;-</span> xx <span class="o">+</span> short.edge <span class="o">-</span> <span class="m">1</span>
  croped <span class="o">&lt;-</span> im<span class="p">[</span>yy<span class="o">:</span>yend<span class="p">,</span> xx<span class="o">:</span>xend<span class="p">,,]</span>
  <span class="c1"># resize to 224 x 224, needed by input of the model.</span>
  resized <span class="o">&lt;-</span> resize<span class="p">(</span>croped<span class="p">,</span> <span class="m">224</span><span class="p">,</span> <span class="m">224</span><span class="p">)</span>
  <span class="c1"># convert to array (x, y, channel)</span>
  arr <span class="o">&lt;-</span> <span class="kp">as.array</span><span class="p">(</span>resized<span class="p">)</span>
  <span class="kp">dim</span><span class="p">(</span>arr<span class="p">)</span> <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="m">224</span><span class="p">,</span> <span class="m">224</span><span class="p">,</span> <span class="m">3</span><span class="p">)</span>
  <span class="c1"># substract the mean</span>
  normed <span class="o">&lt;-</span> arr <span class="o">-</span> mean.img
  <span class="c1"># Reshape to format needed by mxnet (width, height, channel, num)</span>
  <span class="kp">dim</span><span class="p">(</span>normed<span class="p">)</span> <span class="o">&lt;-</span> <span class="kt">c</span><span class="p">(</span><span class="m">224</span><span class="p">,</span> <span class="m">224</span><span class="p">,</span> <span class="m">3</span><span class="p">,</span> <span class="m">1</span><span class="p">)</span>
  <span class="kr">return</span><span class="p">(</span>normed<span class="p">)</span>
<span class="p">}</span></code></pre>
         <p>
          最后我们读入图像，预处理与预测就可以了。
         </p>
         <pre><code class="language-r">im <span class="o">&lt;-</span> load.image<span class="p">(</span>src<span class="p">)</span>
normed <span class="o">&lt;-</span> preproc.image<span class="p">(</span>im<span class="p">,</span> mean.img<span class="p">)</span>
prob <span class="o">&lt;-</span> predict<span class="p">(</span>model<span class="p">,</span> X <span class="o">=</span> normed<span class="p">)</span>
max.idx <span class="o">&lt;-</span> <span class="kp">order</span><span class="p">(</span>prob<span class="p">[,</span><span class="m">1</span><span class="p">],</span> decreasing <span class="o">=</span> <span class="kc">TRUE</span><span class="p">)[</span><span class="m">1</span><span class="o">:</span><span class="m">5</span><span class="p">]</span>
result <span class="o">&lt;-</span> synsets<span class="p">[</span>max.idx<span class="p">]</span></code></pre>
         <h1>
          四、参考资料
         </h1>
         <p>
          MXNet是一个在底层与接口都有着丰富功能的软件，如果读者对它感兴趣，可以参考一些额外的材料来进一步了解MXNet，或者是深度学习这个领域。
         </p>
         <ul>
          <li>
           <a href="https://github.com/dmlc/mxnet">
            MXNet on github
           </a>
          </li>
          <li>
           <a href="http://mxnet.readthedocs.org/en/latest/">
            MXNet完整文档
           </a>
          </li>
          <li>
           <a href="http://mxnet.readthedocs.org/en/latest/R-package/index.html">
            mxnet R包入门文档
           </a>
          </li>
          <li>
           <a href="http://dmlc.ml/rstats/2015/12/08/image-classification-shiny-app-mxnet-r.html">
            结合Shiny+MXNet搭建在线识图服务
           </a>
          </li>
          <li>
           <a href="http://ufldl.stanford.edu/wiki/index.php/UFLDL%E6%95%99%E7%A8%8B">
            深度学习入门
           </a>
          </li>
          <li>
           <a href="http://dmlc.ml/">
            DMLC主页
           </a>
          </li>
         </ul>
         <p>
         </p>
         <div class="wumii-hook">
          <br/>
          <br/>
         </div>
        </div>
        <!-- .entry-content -->
        <footer class="entry-meta">
        </footer>
        <!-- .entry-meta -->
       </article>
       <!-- #post -->
       <nav class="navigation post-navigation" role="navigation">
        <h1 class="screen-reader-text">
         文章导航
        </h1>
        <div class="nav-links">
         <a href="http://cos.name/2016/04/deep-learning/" rel="prev">
          <span class="meta-nav">
           ←
          </span>
          COS每周精选：深度学习
         </a>
         <a href="http://cos.name/2016/04/famous-sayings/" rel="next">
          COS每周精选：名家名言
          <span class="meta-nav">
           →
          </span>
         </a>
        </div>
        <!-- .nav-links -->
       </nav>
       <!-- .navigation -->
       <div class="comments-area" id="comments">
        <h2 class="comments-title">
         《
         <span>
          mxnet：结合R与GPU加速深度学习
         </span>
         》有8个想法
        </h2>
        <ol class="comment-list">
         <li class="pingback even thread-even depth-1" id="comment-7146">
          <div class="comment-body">
           Pingback：
           <a class="url" href="http://www.dazhongdu.com/?p=761" rel="external nofollow">
            大数据与机器学习周报 第5期：Facebook发布了新工具AAT，盲人也可以“看到”图片内容了 | 大众阅读
           </a>
          </div>
         </li>
         <!-- #comment-## -->
         <li class="comment byuser comment-author-yihui odd alt thread-odd thread-alt depth-1 parent" id="comment-7148">
          <article class="comment-body" id="div-comment-7148">
           <footer class="comment-meta">
            <div class="comment-author vcard">
             <img src="http://sdn.geekzu.org/avatar/1022d8e6ebc94e8f6bca9a86cebe312a?s=74&amp;d=monsterid&amp;r=g"/>
             <b class="fn">
              <a class="url" href="http://yihui.name" rel="external nofollow">
               谢益辉
              </a>
             </b>
             <span class="says">
              说道：
             </span>
            </div>
            <!-- .comment-author -->
            <div class="comment-metadata">
             <a href="http://cos.name/2016/04/mxnet-r/#comment-7148">
              <time datetime="2016-04-16T04:11:42+00:00">
               2016/04/16 04:11
              </time>
             </a>
            </div>
            <!-- .comment-metadata -->
           </footer>
           <!-- .comment-meta -->
           <div class="comment-content">
            <p>
             赞，魔王真是颜酷啊！
            </p>
            <p>
             xgboost 和 mxnet 包都有个地方我觉得不是特别方便，就是打印那一长串训练精度，不知道能否改成用折线图的形式展示呢，打印不仅占屏幕空间大（训练完一个模型可能需要往回猛滚鼠标），而且肉眼比较数字大小是一件很困难的事情，用个折线图很容易就能看到训练精度的走向，就像
             <a href="http://playground.tensorflow.org" rel="nofollow">
              http://playground.tensorflow.org
             </a>
             这里面的一样。
            </p>
            <p>
             还有就是每次见魔王写例子我看都会重复一件事情，就是分拆样本为训练集和测试集，觉得有点枯燥。caret 里大概有现成的函数，不过我觉得做个 shiny 应用来做这些事情应该更容易也方便吧，要是 shiny 应用再结合 RStudio 插件，那就满分了，用户只需要在 RStudio 里咣击点两下，拆数据、选参数，然后就可以端杯茶在一边看折线图上的误差刷刷下降了，不然每次都是重复敲类似的代码。
            </p>
           </div>
           <!-- .comment-content -->
           <div class="reply">
            <a aria-label="回复给谢益辉" class="comment-reply-link" href="http://cos.name/2016/04/mxnet-r/?replytocom=7148#respond" onclick='return addComment.moveForm( "div-comment-7148", "7148", "respond", "11789" )' rel="nofollow">
             回复
            </a>
           </div>
          </article>
          <!-- .comment-body -->
          <ol class="children">
           <li class="comment byuser comment-author-hetong007 bypostauthor even depth-2 parent" id="comment-7149">
            <article class="comment-body" id="div-comment-7149">
             <footer class="comment-meta">
              <div class="comment-author vcard">
               <img src="http://sdn.geekzu.org/avatar/b49d125d112a01106b48a2fcda481c3e?s=74&amp;d=monsterid&amp;r=g"/>
               <b class="fn">
                <a class="url" href="http://blog.programet.org/" rel="external nofollow">
                 严酷的魔王
                </a>
               </b>
               <span class="says">
                说道：
               </span>
              </div>
              <!-- .comment-author -->
              <div class="comment-metadata">
               <a href="http://cos.name/2016/04/mxnet-r/#comment-7149">
                <time datetime="2016-04-16T15:03:50+00:00">
                 2016/04/16 15:03
                </time>
               </a>
              </div>
              <!-- .comment-metadata -->
             </footer>
             <!-- .comment-meta -->
             <div class="comment-content">
              <p>
               折线图是个好想法！
              </p>
              <p>
               之前dmlc尝试过做一个在线图像识别的shiny应用：
               <a href="http://dmlc.ml/rstats/2015/12/08/image-classification-shiny-app-mxnet-r.html。" rel="nofollow">
                http://dmlc.ml/rstats/2015/12/08/image-classification-shiny-app-mxnet-r.html。
               </a>
               但是后来发现免费版的shiny只能单线程，服务器承受不了于是就没有做一个这样的demo出来。
              </p>
             </div>
             <!-- .comment-content -->
             <div class="reply">
              <a aria-label="回复给严酷的魔王" class="comment-reply-link" href="http://cos.name/2016/04/mxnet-r/?replytocom=7149#respond" onclick='return addComment.moveForm( "div-comment-7149", "7149", "respond", "11789" )' rel="nofollow">
               回复
              </a>
             </div>
            </article>
            <!-- .comment-body -->
            <ol class="children">
             <li class="comment byuser comment-author-yihui odd alt depth-3" id="comment-7150">
              <article class="comment-body" id="div-comment-7150">
               <footer class="comment-meta">
                <div class="comment-author vcard">
                 <img src="http://sdn.geekzu.org/avatar/1022d8e6ebc94e8f6bca9a86cebe312a?s=74&amp;d=monsterid&amp;r=g"/>
                 <b class="fn">
                  <a class="url" href="http://yihui.name" rel="external nofollow">
                   谢益辉
                  </a>
                 </b>
                 <span class="says">
                  说道：
                 </span>
                </div>
                <!-- .comment-author -->
                <div class="comment-metadata">
                 <a href="http://cos.name/2016/04/mxnet-r/#comment-7150">
                  <time datetime="2016-04-17T10:41:21+00:00">
                   2016/04/17 10:41
                  </time>
                 </a>
                </div>
                <!-- .comment-metadata -->
               </footer>
               <!-- .comment-meta -->
               <div class="comment-content">
                <p>
                 我的意思不是一个具体的 shiny 应用案例，而是一个类似使用向导的东西，选数据、拆、选参数、训练、预测之类的。这个 shiny 应用放在包里，用户可以在本地跑就可以了，不需要部署出去。
                </p>
                <p>
                 折线图可能会有点耗时间，另一个可能的办法是用古董的打印文本方式，比如文本进度条（关键就是 \r 字符），这样显示应该会快很多，用户也可以很直观看见训练的精度变化。
                </p>
               </div>
               <!-- .comment-content -->
              </article>
              <!-- .comment-body -->
             </li>
             <!-- #comment-## -->
             <li class="comment byuser comment-author-hetong007 bypostauthor even depth-3" id="comment-7151">
              <article class="comment-body" id="div-comment-7151">
               <footer class="comment-meta">
                <div class="comment-author vcard">
                 <img src="http://sdn.geekzu.org/avatar/b49d125d112a01106b48a2fcda481c3e?s=74&amp;d=monsterid&amp;r=g"/>
                 <b class="fn">
                  <a class="url" href="http://blog.programet.org/" rel="external nofollow">
                   严酷的魔王
                  </a>
                 </b>
                 <span class="says">
                  说道：
                 </span>
                </div>
                <!-- .comment-author -->
                <div class="comment-metadata">
                 <a href="http://cos.name/2016/04/mxnet-r/#comment-7151">
                  <time datetime="2016-04-17T14:34:05+00:00">
                   2016/04/17 14:34
                  </time>
                 </a>
                </div>
                <!-- .comment-metadata -->
               </footer>
               <!-- .comment-meta -->
               <div class="comment-content">
                <p>
                 忽然觉得，如果这个shiny能放在html版的vignettes里面会挺有意思的
                </p>
               </div>
               <!-- .comment-content -->
              </article>
              <!-- .comment-body -->
             </li>
             <!-- #comment-## -->
             <li class="comment odd alt depth-3" id="comment-7249">
              <article class="comment-body" id="div-comment-7249">
               <footer class="comment-meta">
                <div class="comment-author vcard">
                 <img src="http://sdn.geekzu.org/avatar/436174399b7aaeb22c666bc623015a5c?s=74&amp;d=monsterid&amp;r=g"/>
                 <b class="fn">
                  <a class="url" href="http://www.xueningzhu.com/" rel="external nofollow">
                   布丁Nnn
                  </a>
                 </b>
                 <span class="says">
                  说道：
                 </span>
                </div>
                <!-- .comment-author -->
                <div class="comment-metadata">
                 <a href="http://cos.name/2016/04/mxnet-r/#comment-7249">
                  <time datetime="2016-05-24T18:07:08+00:00">
                   2016/05/24 18:07
                  </time>
                 </a>
                </div>
                <!-- .comment-metadata -->
               </footer>
               <!-- .comment-meta -->
               <div class="comment-content">
                <p>
                 没有ddl就没有生产力……
                </p>
               </div>
               <!-- .comment-content -->
              </article>
              <!-- .comment-body -->
             </li>
             <!-- #comment-## -->
            </ol>
            <!-- .children -->
           </li>
           <!-- #comment-## -->
           <li class="comment even depth-2" id="comment-7152">
            <article class="comment-body" id="div-comment-7152">
             <footer class="comment-meta">
              <div class="comment-author vcard">
               <img src="http://sdn.geekzu.org/avatar/958f8986fe42c6cda2d976b3dc0aa77e?s=74&amp;d=monsterid&amp;r=g"/>
               <b class="fn">
                寇强
               </b>
               <span class="says">
                说道：
               </span>
              </div>
              <!-- .comment-author -->
              <div class="comment-metadata">
               <a href="http://cos.name/2016/04/mxnet-r/#comment-7152">
                <time datetime="2016-04-19T07:10:10+00:00">
                 2016/04/19 07:10
                </time>
               </a>
              </div>
              <!-- .comment-metadata -->
             </footer>
             <!-- .comment-meta -->
             <div class="comment-content">
              <p>
               H2O有个模块叫h2o flow，就是这么做的，深受众多MBA的喜爱。
              </p>
             </div>
             <!-- .comment-content -->
             <div class="reply">
              <a aria-label="回复给寇强" class="comment-reply-link" href="http://cos.name/2016/04/mxnet-r/?replytocom=7152#respond" onclick='return addComment.moveForm( "div-comment-7152", "7152", "respond", "11789" )' rel="nofollow">
               回复
              </a>
             </div>
            </article>
            <!-- .comment-body -->
           </li>
           <!-- #comment-## -->
          </ol>
          <!-- .children -->
         </li>
         <!-- #comment-## -->
         <li class="comment odd alt thread-even depth-1" id="comment-7188">
          <article class="comment-body" id="div-comment-7188">
           <footer class="comment-meta">
            <div class="comment-author vcard">
             <img src="http://sdn.geekzu.org/avatar/3d82f80095f636ae2bdecf96c9581eaf?s=74&amp;d=monsterid&amp;r=g"/>
             <b class="fn">
              bird 张翔
             </b>
             <span class="says">
              说道：
             </span>
            </div>
            <!-- .comment-author -->
            <div class="comment-metadata">
             <a href="http://cos.name/2016/04/mxnet-r/#comment-7188">
              <time datetime="2016-04-25T09:01:57+00:00">
               2016/04/25 09:01
              </time>
             </a>
            </div>
            <!-- .comment-metadata -->
           </footer>
           <!-- .comment-meta -->
           <div class="comment-content">
            <p>
             最近想山寨alphago的策略 用mxnet搞旅行者问题（图的最短距离遍历）
             <br/>
             <a href="https://en.wikipedia.org/wiki/Travelling_salesman_problem#Exact_algorithms" rel="nofollow">
              https://en.wikipedia.org/wiki/Travelling_salesman_problem#Exact_algorithms
             </a>
            </p>
            <p>
             自己生成了10000个22个城市节点的样本，然后去训练几个神经网络
             <br/>
             1、预测遍历的起点
             <br/>
             2、遍历的下一步
             <br/>
             3、预测最短路径的长度
             <br/>
             打算用这两个网络去优化路径的搜素过程，和下围棋还是蛮像的
            </p>
            <p>
             结果参数搜索到6层神经网络，训练集效果没有变好的趋势
            </p>
            <p>
             ╮(╯▽╰)╭ 不知道该不该继续下去
             <br/>
             继续添加训练样本成本也不小，原始算法复杂度至少2^n
            </p>
            <p>
             不过有个比较明显的问题，就是两个节点间有链接的话，边的距离大部分10以内，对于无连接的边，用Inf 或者一个较大的数，对于网络权重的训练 好像都不是很好，目前卡在这里了╮(╯▽╰)╭
            </p>
           </div>
           <!-- .comment-content -->
           <div class="reply">
            <a aria-label="回复给bird 张翔" class="comment-reply-link" href="http://cos.name/2016/04/mxnet-r/?replytocom=7188#respond" onclick='return addComment.moveForm( "div-comment-7188", "7188", "respond", "11789" )' rel="nofollow">
             回复
            </a>
           </div>
          </article>
          <!-- .comment-body -->
         </li>
         <!-- #comment-## -->
        </ol>
        <!-- .comment-list -->
        <div class="comment-respond" id="respond">
         <h3 class="comment-reply-title" id="reply-title">
          发表评论
          <small>
           <a href="http://cos.name/2016/04/mxnet-r/#respond" id="cancel-comment-reply-link" rel="nofollow" style="display:none;">
            取消回复
           </a>
          </small>
         </h3>
         <form action="http://cos.name/wp-comments-post.php" class="comment-form" id="commentform" method="post" novalidate="">
          <p class="comment-notes">
           <span id="email-notes">
            电子邮件地址不会被公开。
           </span>
           必填项已用
           <span class="required">
            *
           </span>
           标注
          </p>
          <p class="comment-form-comment">
           <label for="comment">
            评论
           </label>
           <textarea aria-required="true" cols="45" id="comment" maxlength="65525" name="comment" required="required" rows="8">
           </textarea>
          </p>
          <p class="comment-form-author">
           <label for="author">
            姓名
            <span class="required">
             *
            </span>
           </label>
          </p>
          <p class="comment-form-email">
           <label for="email">
            电子邮件
            <span class="required">
             *
            </span>
           </label>
          </p>
          <p class="comment-form-url">
           <label for="url">
            站点
           </label>
          </p>
          <p class="form-submit">
          </p>
          <p style="display: none;">
          </p>
          <p style="display: none;">
          </p>
         </form>
        </div>
        <!-- #respond -->
       </div>
       <!-- #comments -->
      </div>
      <!-- #content -->
     </div>
     <!-- #primary -->
     <div class="sidebar-container" id="tertiary" role="complementary">
      <div class="sidebar-inner">
       <div class="widget-area">
        <aside class="widget widget_text" id="text-3">
         <h3 class="widget-title">
          关注统计之都
         </h3>
         <div class="textwidget">
          <ul>
           <li>
            新浪微博
            <a href="http://weibo.com/cosname">
             @统计之都
            </a>
           </li>
           <li>
            人人网
            <a href="http://renren.com/cosname">
             @统计之都
            </a>
           </li>
           <li>
            Twitter
            <a href="http://twitter.com/cos_name">
             @cos_name
            </a>
           </li>
          </ul>
         </div>
        </aside>
        <aside class="widget widget_text" id="text-6">
         <h3 class="widget-title">
          微信公众平台
         </h3>
         <div class="textwidget">
          <img src="http://cos.name/wp-content/uploads/2013/04/qrcode-8cm.jpg"/>
          <p style="font-size:12px;margin-left:15px">
           微信号 CapStat
          </p>
          <p>
           我们将第一时间向您推送主站和论坛的精彩内容，以及统计之都的线下活动、竞赛、培训和会议信息。
          </p>
         </div>
        </aside>
        <aside class="widget widget_text" id="text-8">
         <h3 class="widget-title">
          站内导航
         </h3>
         <div class="textwidget">
          <ul>
           <li class="page_item">
            <a href="http://cos.name/cn/">
             中文论坛
            </a>
           </li>
           <li class="page_item">
            <a href="http://cos.name/videos/">
             视频
            </a>
           </li>
           <li class="page_item">
            <a href="http://cos.name/salon/">
             统计沙龙
            </a>
           </li>
           <li class="page_item">
            <a href="http://cos.name/books/">
             图书出版
            </a>
           </li>
           <li class="page_item">
            <a href="http://cos.name/training/">
             教育培训
            </a>
           </li>
           <li class="page_item">
            <a href="http://cos.name/about/">
             关于我们
            </a>
           </li>
           <li class="page_item">
            <a href="http://cos.name/donate/">
             捐赠
            </a>
           </li>
           <li class="page_item">
            <a href="http://cos.name/chinar/">
             R语言会议
            </a>
           </li>
          </ul>
         </div>
        </aside>
        <aside class="widget widget_links" id="linkcat-2">
         <h3 class="widget-title">
          友情链接
         </h3>
         <ul class="xoxo blogroll">
          <li>
           <a href="http://stat.ruc.edu.cn" target="_blank" title="中国人民大学统计学院网站">
            中国人民大学统计学院
           </a>
          </li>
          <li>
           <a href="http://rucdmc.net">
            中国人民大学数据挖掘中心
           </a>
          </li>
          <li>
           <a href="http://birc.gsm.pku.edu.cn/" target="_blank">
            北京大学商务智能研究中心
           </a>
          </li>
          <li>
           <a href="http://sam.cufe.edu.cn/" target="_blank" title="中央财经大学统计与数学学院网站">
            中央财经大学统计与数学学院
           </a>
          </li>
          <li>
           <a href="http://tjx.cueb.edu.cn/" target="_blank" title="首都经济贸易大学统计学院网站">
            首经贸统计学院
           </a>
          </li>
          <li>
           <a href="http://www.shookr.com/">
            数客网大数据社区
           </a>
          </li>
          <li>
           <a href="http://www.xueqing.tv/" target="_blank" title="数据科学在线学习平台">
            雪晴数据网
           </a>
          </li>
          <li>
           <a href="http://iera.name/" target="_blank" title="IERA是一个旨在普及、传播和增进工业工程知识的非营利性网站">
            IERA（直通IE）
           </a>
          </li>
         </ul>
        </aside>
        <aside class="widget widget_categories" id="categories-2">
         <h3 class="widget-title">
          全部分类
         </h3>
         <label class="screen-reader-text" for="cat">
          全部分类
         </label>
         <select class="postform" id="cat" name="cat">
          <option value="-1">
           选择分类目录
          </option>
          <option class="level-0" value="925">
           cos访谈  (4)
          </option>
          <option class="level-0" value="659">
           图书出版  (5)
          </option>
          <option class="level-0" value="379">
           数学方法  (14)
          </option>
          <option class="level-1" value="381">
           分析与代数  (1)
          </option>
          <option class="level-1" value="380">
           概率论  (9)
          </option>
          <option class="level-1" value="382">
           随机过程  (5)
          </option>
          <option class="level-0" value="210">
           数据分析  (81)
          </option>
          <option class="level-1" value="203">
           多元统计  (3)
          </option>
          <option class="level-1" value="42">
           数据挖掘与机器学习  (42)
          </option>
          <option class="level-1" value="36">
           生物与医学统计  (17)
          </option>
          <option class="level-1" value="35">
           计量经济学  (4)
          </option>
          <option class="level-1" value="296">
           金融统计  (3)
          </option>
          <option class="level-1" value="303">
           风险精算  (7)
          </option>
          <option class="level-0" value="177">
           模型专题  (15)
          </option>
          <option class="level-1" value="38">
           回归分析  (10)
          </option>
          <option class="level-1" value="41">
           时间序列  (2)
          </option>
          <option class="level-0" value="784">
           每周精选  (24)
          </option>
          <option class="level-1" value="183">
           可视化  (9)
          </option>
          <option class="level-0" value="967">
           沙龙纪要  (3)
          </option>
          <option class="level-0" value="18">
           经典理论  (46)
          </option>
          <option class="level-1" value="37">
           抽样调查  (3)
          </option>
          <option class="level-1" value="4">
           统计推断  (26)
          </option>
          <option class="level-1" value="236">
           试验设计  (7)
          </option>
          <option class="level-1" value="39">
           非参数统计  (3)
          </option>
          <option class="level-0" value="1">
           统计之都  (279)
          </option>
          <option class="level-1" value="884">
           中国R会议  (2)
          </option>
          <option class="level-1" value="885">
           中国R语言会议  (5)
          </option>
          <option class="level-1" value="446">
           出国留学  (3)
          </option>
          <option class="level-1" value="179">
           推荐文章  (90)
          </option>
          <option class="level-1" value="3">
           新闻通知  (75)
          </option>
          <option class="level-1" value="263">
           统计刊物  (10)
          </option>
          <option class="level-1" value="174">
           网站导读  (40)
          </option>
          <option class="level-1" value="204">
           职业事业  (51)
          </option>
          <option class="level-1" value="213">
           高校课堂  (9)
          </option>
          <option class="level-0" value="178">
           统计计算  (28)
          </option>
          <option class="level-1" value="40">
           优化与模拟  (15)
          </option>
          <option class="level-1" value="43">
           贝叶斯方法  (6)
          </option>
          <option class="level-0" value="378">
           软件应用  (116)
          </option>
          <option class="level-1" value="44">
           统计图形  (36)
          </option>
          <option class="level-1" value="110">
           统计软件  (83)
          </option>
         </select>
        </aside>
        <aside class="widget widget_recent_comments" id="recent-comments-2">
         <h3 class="widget-title">
          最新评论
         </h3>
         <ul id="recentcomments">
          <li class="recentcomments">
           <span class="comment-author-link">
            fineboom
           </span>
           发表在《
           <a href="http://cos.name/2016/06/use-shiny-fleetly-set-up-visual-prototype-system/#comment-7317">
            利用shiny包快速搭建可视化原型系统
           </a>
           》
          </li>
          <li class="recentcomments">
           <span class="comment-author-link">
            胡家新
           </span>
           发表在《
           <a href="http://cos.name/2016/06/r%e8%af%ad%e5%8d%83%e5%af%bb%e7%ac%ac%e4%b8%89%e6%9c%9f%ef%bc%9a%e5%bc%a0%e6%97%a0%e5%bf%8c%e7%a9%b6%e7%ab%9f%e7%88%b1%e8%b0%81%ef%bc%9f/#comment-7316">
            R语千寻第三期：张无忌究竟爱谁？
           </a>
           》
          </li>
          <li class="recentcomments">
           <span class="comment-author-link">
            <a class="url" href="http://www.zijiacha.com/category.php?id=6" rel="external nofollow">
             南糯山普洱茶
            </a>
           </span>
           发表在《
           <a href="http://cos.name/2016/06/r%e8%af%ad%e5%8d%83%e5%af%bb%e7%ac%ac%e4%b8%89%e6%9c%9f%ef%bc%9a%e5%bc%a0%e6%97%a0%e5%bf%8c%e7%a9%b6%e7%ab%9f%e7%88%b1%e8%b0%81%ef%bc%9f/#comment-7315">
            R语千寻第三期：张无忌究竟爱谁？
           </a>
           》
          </li>
          <li class="recentcomments">
           <span class="comment-author-link">
            J
           </span>
           发表在《
           <a href="http://cos.name/2016/05/value-of-the-reputation-from-the-data/#comment-7314">
            数据告诉你：高信誉的卖家应该收高价，还是收低价？
           </a>
           》
          </li>
          <li class="recentcomments">
           <span class="comment-author-link">
            <a class="url" href="http://gg" rel="external nofollow">
             Hilda
            </a>
           </span>
           发表在《
           <a href="http://cos.name/2013/01/drawing-map-in-r-era/#comment-7311">
            R时代，你要怎样画地图？
           </a>
           》
          </li>
         </ul>
        </aside>
        <aside class="widget widget_rss" id="rss-282869971">
         <h3 class="widget-title">
          <a class="rsswidget" href="http://cos.name/cn/topics/feed/">
           <img src="http://cos.name/wp-includes/images/rss.png"/>
          </a>
          <a class="rsswidget" href="http://cos.name/cn/topics/feed/">
           中文论坛新帖
          </a>
         </h3>
         <ul>
          <li>
           <a class="rsswidget" href="http://cos.name/cn/topic/417372/">
            处理时间数据和产生时间序列的问题
           </a>
          </li>
          <li>
           <a class="rsswidget" href="http://cos.name/cn/topic/6790/">
            《统计陷阱》下载 （How to lie with statistics）
           </a>
          </li>
          <li>
           <a class="rsswidget" href="http://cos.name/cn/topic/818/">
            统计学的世界（第五版）
           </a>
          </li>
          <li>
           <a class="rsswidget" href="http://cos.name/cn/topic/16574/">
            class(x) 返回值值是AsIs,AsIs代表什么，有什么用处？
           </a>
          </li>
          <li>
           <a class="rsswidget" href="http://cos.name/cn/topic/417366/">
            如何用R绘制一个分类算法的决策规则
           </a>
          </li>
         </ul>
        </aside>
        <aside class="widget widget_text" id="text-2">
         <h3 class="widget-title">
          登录/RSS
         </h3>
         <div class="textwidget">
          <ul>
           <li>
            <a href="http://cos.name/wp-admin/">
             登录
            </a>
           </li>
           <li>
            <a href="http://cos.name/feed/" title="使用 RSS 2.0 同步站点内容">
             文章
             <abbr title="Really Simple Syndication">
              RSS
             </abbr>
            </a>
           </li>
           <li>
            <a href="http://cos.name/comments/feed/" title="RSS 上的最近评论">
             评论
             <abbr title="Really Simple Syndication">
              RSS
             </abbr>
            </a>
           </li>
          </ul>
         </div>
        </aside>
       </div>
       <!-- .widget-area -->
      </div>
      <!-- .sidebar-inner -->
     </div>
     <!-- #tertiary -->
    </div>
    <!-- #main -->
    <footer class="site-footer" id="colophon" role="contentinfo">
     <div class="site-info">
      版权所有 © 2014 统计之都 | 由
      <a href="http://wordpress.org/">
       WordPress
      </a>
      构建 | 主题修改自
      <a href="http://wordpress.org/themes/twentythirteen">
       Twenty Thirteen
      </a>
     </div>
     <!-- .site-info -->
    </footer>
    <!-- #colophon -->
   </div>
   <!-- #page -->
   <p style="margin:0;padding:0;height:1px;overflow:hidden;">
    <a href="http://www.wumii.com/widget/relatedItems" style="border:0;">
     <img src="http://static.wumii.cn/images/pixel.png"/>
    </a>
   </p>
  </div>
 </body>
</html>